//
//  ReadingView.m
//  CollectionViewDemo
//
//  Created by liuchengming on 16/12/10.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "ReadingView.h"
#import "CollectionViewController.h"

@interface ReadingView () <UITextViewDelegate>
{
   // UITextView * textView;
    int currentPage;
    NSMutableArray * bookName;
}


@end

@implementation ReadingView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Btn];
    [self getBookName];
    //[self Text];
    
    //
   // UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onUpPege:)];
    //[textView addGestureRecognizer:pan];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//文字
- (void)Textdata:(NSInteger)intger
{
    _textView = [UITextView new];
    
    _textView.frame = CGRectMake(0,20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44-20);
    
    _textView.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:bookName[intger] ofType:@".txt"] encoding:NSUTF8StringEncoding error:nil];
    
    _textView.editable = NO;
    _textView.inputAccessoryView = [UIImageView new];
    [self.view addSubview:_textView];
    
    currentPage = _textView.contentSize.height /([UIScreen mainScreen].bounds.size.height-44)- 1;
    NSLog(@"pege======%d",currentPage);
    
}



//按钮
- (void)Btn
{
    UIToolbar * toolbar = [UIToolbar new];
    toolbar.tintColor = [UIColor redColor];
    // 宽 高 宽 高
    toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44,[UIScreen mainScreen].bounds.size.width, 44);
    [self.view addSubview:toolbar];
    
    UIBarButtonItem * button01 = [[UIBarButtonItem alloc] initWithTitle:@"my book" style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    button01.tintColor = [UIColor blueColor];
    NSArray *arr = @[button01];
    [toolbar setItems:arr animated:YES];
    
    
}

- (void)fanhui
{
    //[self dismissViewControllerAnimated:YES completion:^{}];
    
    //self.navigationController.toolbarHidden = YES;
    NSInteger i = _textView.contentSize.height /430+1;
    NSLog(@"%ld",(long)i);
    
}


//UPPege
/*
- (void)onUpPege:(UIPanGestureRecognizer *)pan
{
    CGPoint  point = [pan translationInView:textView];
    if (point.x < 3) {
        [pan setTranslation:CGPointMake(0, 0) inView:textView];
        
        [textView setContentOffset:CGPointMake(0, (currentPage - 1) * [UIScreen mainScreen].bounds.size.height - 44) animated:YES];
        
    }else if (point.x > -3 ) {
        [pan setTranslation:CGPointMake(0, 0) inView:textView];
        
        [textView setContentOffset:CGPointMake(0, (currentPage + 1) * 430) animated:YES];
    }
    [pan setTranslation:CGPointMake(0, 0) inView:textView];
    NSLog(@"%f",point.x);
}
 */


- (void)getBookName
{
    NSArray <NSURL *> * jpg = [[NSBundle mainBundle] URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    NSString * str = [NSString stringWithFormat:@"%@",jpg];
    NSArray * arrs = [str componentsSeparatedByString:@","];
    //NSLog(@"%@",stri);
    bookName = [NSMutableArray new];
    for (NSString * line in arrs)
    {
        NSArray * arr = [line componentsSeparatedByString:@".jpeg"];
        NSString * str = [NSString stringWithFormat:@"%@",arr.firstObject];
        arr = [str componentsSeparatedByString:@" "];
        str = [NSString stringWithFormat:@"%@",arr];
        arr = [str componentsSeparatedByString:@"\""];
        str = [NSString stringWithFormat:@"%@",arr[10]];
        [bookName addObject:str];
        //NSLog(@"%lu",(unsigned long)bookName.count);
        NSLog(@"%@",str);
    }
}



@end
